home *** CD-ROM | disk | FTP | other *** search
/ Amiga Format CD 42 / Amiga Format AFCD42 (Issue 126, Aug 1999).iso / -serious- / programming / other / jikes / src / option.cpp < prev    next >
C/C++ Source or Header  |  1999-05-14  |  17KB  |  480 lines

  1. // $Id: option.cpp,v 1.7 1999/03/10 19:59:21 shields Exp $
  2. //
  3. // This software is subject to the terms of the IBM Jikes Compiler
  4. // License Agreement available at the following URL:
  5. // http://www.ibm.com/research/jikes.
  6. // Copyright (C) 1996, 1998, International Business Machines Corporation
  7. // and others.  All Rights Reserved.
  8. // You must accept the terms of that agreement to use this software.
  9. //
  10.  
  11. #include <ctype.h>
  12. #include "config.h"
  13. #include "option.h"
  14. #include "javasym.h"
  15. #include "error.h"
  16.  
  17. //
  18. //
  19. //
  20. bool ArgumentExpander::ArgumentExpanded(Tuple<char *> &arguments, char *file_name)
  21. {
  22.     struct stat status;
  23.     FILE *afile = fopen(file_name, "r");
  24.     if (afile && (::SystemStat(file_name, &status) == 0))
  25.     {
  26.         char *buffer = new char[status.st_size + 2];
  27.         int file_size = fread(buffer, 1, status.st_size, afile);
  28.         buffer[file_size] = '\n';
  29.         for (int k = 0; k < file_size; k++)
  30.         {
  31.             //
  32.             // isgraph(c) is true if c is any printing character except space.
  33.             //
  34.             while ((! isgraph(buffer[k])) && buffer[k] != '\n' && buffer[k] != '\r')
  35.                 k++;
  36.             if (buffer[k] != '\n' && buffer[k] != '\r')
  37.             {
  38.                 int n;
  39.                 for (n = k + 1; buffer[n] != '\n' && buffer[n] != '\r'; n++)
  40.                     ;
  41.                 buffer[n] = U_NULL;
  42.                 char *str = new char[n - k + 1];
  43.                 strcpy(str, &buffer[k]);
  44.                 arguments.Next() = str;
  45.                 k = n;
  46.             }
  47.         }
  48.         delete [] buffer;
  49.         fclose(afile);
  50.  
  51.         return true;
  52.     }
  53.  
  54.     return false;
  55. }
  56.  
  57.  
  58. ArgumentExpander::ArgumentExpander(int argc_, char *argv_[])
  59. {
  60.     Tuple<char *> arguments(8192);
  61.     for (int i = 0; i < argc_; i++)
  62.     {
  63.         char *argument = argv_[i];
  64.         if (argument[0] != '@' || (! ArgumentExpanded(arguments, argument + 1)))
  65.         {
  66.             char *str = new char[strlen(argument) + 1];
  67.             strcpy(str, argument);
  68.             arguments.Next() = str;
  69.         }
  70.     }
  71.  
  72.     argc = arguments.Length();
  73.     argv = new char*[argc];
  74.  
  75.     for (int k = 0; k < argc; k++)
  76.         argv[k] = arguments[k];
  77.  
  78.     return;
  79. }
  80.  
  81.  
  82. ArgumentExpander::ArgumentExpander(Tuple<char> &line)
  83. {
  84.     Tuple<char *> arguments(8192);
  85.  
  86.     int end = 0; 
  87.     do
  88.     {
  89.         for (; end < line.Length() && line[end] == U_SPACE; end++)
  90.             ;
  91.         if (end < line.Length())
  92.         {
  93.             int start = end;
  94.             for (end++; end < line.Length() && line[end] != U_SPACE; end++)
  95.                 ;
  96.             int length = end - start;
  97.             char *argument = new char[length + 1];
  98.             for (int i = 0, k = start; k < end; i++, k++)
  99.                 argument[i] = line[k];
  100.             argument[length] = U_NULL;
  101.  
  102.             if (argument[0] == '@' && ArgumentExpanded(arguments, argument + 1))
  103.                  delete argument;
  104.             else arguments.Next() = argument;
  105.         }
  106.     } while(end < line.Length());
  107.  
  108.     argc = arguments.Length();
  109.     argv = new char*[argc];
  110.  
  111.     for (int k = 0; k < argc; k++)
  112.         argv[k] = arguments[k];
  113.  
  114.     return;
  115. }
  116.  
  117.  
  118. #ifdef WIN32_FILE_SYSTEM
  119. void Option::SaveCurrentDirectoryOnDisk(char c)
  120. {
  121.     if (! current_directory[c])
  122.     {
  123.         char *disk_directory = NULL,
  124.              disk[3] = { c, U_COLON, U_NULL },
  125.              tmp[1];
  126.  
  127.         if (SetCurrentDirectory(disk))
  128.         {
  129.             DWORD directory_length = GetCurrentDirectory(0, tmp);  // first, get the right size
  130.             disk_directory = new char[directory_length + 1];       // allocate the directory
  131.             DWORD length = GetCurrentDirectory(directory_length, disk_directory);
  132.             if (length <= directory_length)
  133.             {
  134.                 for (char *ptr = disk_directory; *ptr; ptr++)
  135.                     *ptr = (*ptr != U_BACKSLASH ? *ptr : (char) U_SLASH); // turn '\' to '/'.
  136.             }
  137.         }
  138.  
  139.         if (! disk_directory)
  140.         {
  141.             disk_directory = new char[2];
  142.             strcpy(disk_directory, StringConstant::U8S__DO_);
  143.         }
  144.  
  145.         current_directory[Case::ToAsciiLower(c)] = disk_directory;
  146.         current_directory[Case::ToAsciiUpper(c)] = disk_directory;
  147.     }
  148.  
  149.     return;
  150. }
  151. #endif
  152.  
  153.  
  154. Option::Option(ArgumentExpander &arguments) : default_path(NULL),
  155.                                               classpath(NULL),
  156.                                               makefile_name(NULL),
  157.                                               debug_dump_lex(false),
  158.                                               debug_dump_ast(false),
  159.                                               debug_dump_class(false),
  160.                                               debug_trap_op(false),
  161.                                               applet_author(false),
  162.                                               incremental(false),
  163.                                               makefile(false),
  164.                                               bytecode(true),
  165.                                               full_check(false),
  166.                                               unzip(false),
  167.                                               dump_errors(false),
  168.                                               errors(true),
  169.                                               ascii(false),
  170.                                               comments(false),
  171.                                               pedantic(false),
  172.                                               directory(NULL),
  173.                                               first_file_index(arguments.argc),
  174.                                               one_one(true),
  175.                                               g(false),
  176.                                               nowrite(false),
  177.                                               deprecation(false),
  178.                                               verbose(false),
  179.                                               depend(false),
  180.                                               nowarn(false),
  181.                                               O(false),
  182.                                               zero_defect(false)
  183. {
  184. #ifdef WIN32_FILE_SYSTEM
  185.     for (int j = 0; j < 128; j++)
  186.          current_directory[j] = NULL;
  187.  
  188.     char tmp[1];
  189.     DWORD directory_length = GetCurrentDirectory(0, tmp); // first, get the right size
  190.     char *main_current_directory = new char[directory_length + 1];   // allocate the directory
  191.     DWORD length = GetCurrentDirectory(directory_length, main_current_directory);
  192.     if (length > directory_length)
  193.     {
  194.         delete [] main_current_directory;
  195.         main_current_directory = StringConstant::U8S__DO_;
  196.         main_disk = 0;
  197.     }
  198.     else
  199.     {
  200.         for (char *ptr = main_current_directory; *ptr; ptr++)
  201.             *ptr = (*ptr != U_BACKSLASH ? *ptr : (char) U_SLASH); // turn '\' to '/'.
  202.         main_disk = main_current_directory[0]; // the first character
  203.         current_directory[Case::ToAsciiLower(main_disk)] = main_current_directory;
  204.         current_directory[Case::ToAsciiUpper(main_disk)] = main_current_directory;
  205.     }
  206.     current_directory[0] = main_current_directory;
  207. #endif
  208.  
  209.     Tuple<int> filename_index(2048);
  210.  
  211.     for (int i = 1; i < arguments.argc; i++)
  212.     {
  213.         if (arguments.argv[i][0] == '-')
  214.         {
  215.             if (strcmp(arguments.argv[i],"-classpath") == 0 && ((i + 1) < arguments.argc))
  216.             {
  217.                 classpath = arguments.argv[++i];
  218. #ifdef EBCDIC
  219.                 //
  220.                 //  Maintain CLASSPATH in ASCII and translate back to EBCDIC when building file name
  221.                 //
  222.                 for (int k = 0; k < strlen(classpath); k++)
  223.                     classpath[k] = Code::ToASCII(classpath[k]);
  224. #endif
  225.             }
  226.             else if (strcmp(arguments.argv[i], "-depend") == 0)
  227.                  depend = true;
  228.             else if (strcmp(arguments.argv[i],"-verbose") == 0) 
  229.                  verbose = true;
  230.             else if (strcmp(arguments.argv[i],"-g") == 0)
  231.                  g = true;
  232.             else if (strcmp(arguments.argv[i], "-O") == 0)
  233.                  O = true;
  234.             else if (strcmp(arguments.argv[i],"-deprecation") == 0)
  235.             {
  236.                  bad_optio